<?php
namespace Home\Controller;
use Think\Controller;
class FourmController extends Controller {
    public function index(){
        $uid=session("uid");
        $upic=M('users')->where(array("uid"=>$uid))->select();
        $newsmodel=M('news');
        $news=$newsmodel->order('publishment desc ')->where(array("satus"=>1))->join('users on news.n_uid=users.uid')->select();
        for ($i = 0; $i < count($news); $i++){
            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
            $news[$i]["code"] = $i + 1;
        }
        if($news){
            $this->assign('list', $news);
            $this->assign('upic', $upic);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Personal/index"));}

    }

    public function detail(){
        $uid=session("uid");
        $id = $_GET['id'];
        $upic=M('users')->where(array("uid"=>$uid))->select();
        $replymodel=M('fourm');
        $reply=$replymodel->order('fid')->where(array("id"=>$id))->join('users on users.uid=fourm.uid')->select();
        for ($i = 0; $i < count($reply); $i++){
            $reply[$i]["code"] = $i + 1;
        }
        $newsmodel = M("news");
        $news=$newsmodel->where(array("id"=>$id))->join('users on news.n_uid=users.uid')->select();

        for ($i = 0; $i < count($news); $i++){

            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }

        if($news){
            $this->assign('list', $news);
            $this->assign('a', $reply);
            $this->assign('upic', $upic);
            $this->display();
        }

        else{ $this->error('获取失败!', U("Home/Fourm/index"));}
    }

public function comment(){
    $uid=session('uid');
    $id=$_GET['id'];
    $comment=$_POST['comment'];
    $cid=$uid;
    $Fourm = M("fourm");
    $data = array('uid'=>$uid,'id'=>$id,'comment'=>$comment,'cid'=>$cid);
    $result = $Fourm->add($data);
    if (false !== $result) {
        $this->success('评论成功');
    } else {
        $this->error('评论失败，请重新评论',U("Home/Fourm/index"));
    }
}


    public function reply(){
        $uid=session('uid');
        $fid=I('post.fid');
        $reply=I('post.reply');
        $data = array('r_uid'=>$uid,'r_fid'=>$fid,'reply'=>$reply);
        M("reply")->add($data);
        $result =M('reply')->where(array("r_fid"=>$fid))->join('users on users.uid=reply.r_uid')->select();

       /*echo json_encode($result, JSON_UNESCAPED_UNICODE);*/
        $this->ajaxReturn ($result);

    }

public function addfriend(){
    $myuid=session('uid');
    $frienduid=$_GET['uid'];
    $a=M('friend')->where(array("friend_uid"=>$myuid,"my_uid"=>$frienduid,'fd_satus'=>'1'))->select();
    $data = array('my_uid'=>$frienduid,'friend_uid'=>$myuid,'fd_satus'=>'-1');
    $add=M('friend')->select();
    if($a){ $this->error('对方已经是你的好友啦');}
    elseif($myuid==$frienduid){ $this->error('您已经是您自己最亲密的好友了');}
    elseif ($myuid){
        M("friend")->add($data);
        $this->success('发送请求成功,请等待对方验证');
    }
    else{
        $this->error('添加失败，请重新添加');
    }
}





}


